home *** CD-ROM | disk | FTP | other *** search
- Path: mac007016.shef.ac.uk!user
- From: m.b.greenwood@sheffield.ac.uk (Mike Greenwood)
- Newsgroups: comp.lang.c
- Subject: Re: Please help me
- Date: Tue, 26 Mar 1996 11:47:33 +0000
- Organization: University of Sheffield
- Message-ID: <m.b.greenwood-2603961147330001@mac007016.shef.ac.uk>
- References: <4j6nrl$lfk@badger.wmin.ac.uk>
- NNTP-Posting-Host: mac007016.shef.ac.uk
-
- In article <4j6nrl$lfk@badger.wmin.ac.uk>, darec@westminster.ac.uk
- (Nadarajah Thavaneethan) wrote:
-
- > #define SIZE 60
-
- > void main(void)
- > {
- >
- > char c[SIZE];
- > int index, next;
- >
- etc
- > compchar(c[next-1],c[next]);
-
- > printf("\n the line is now %s\n",c);
- > }
- >
- > void compchar (char c1, char c2)
- > {
- >
- > char p;
- >
- >
- > if (c1 > c2)
- > p = c1;
- > c1 = c2;
- > c2 = p;
- > }
-
- > This program reads a line of data and sorts it into ascending ASCII sequence
- > it doesn't but it should.
-
- Hi there,
-
- I think this is quite a simple problem. When you call the function
- compchar, and swap if necessary, only a copy of c1 and c2 is sent to the
- function. This means that c[next-1] and c[next] in the main() function
- don't get swapped even if c1 and c2 do. One way to avoid this is to
- declare c[SIZE] as global, i.e. before main(), and call 'void
- compchar(void)'.
- Inside the compchar() function you could then operate with:
- if (c[next-1] > c[next])
- etc...
-
- Another way around this is to use pointers, because pointers point to the
- real values whatever, and a copy of a pointer still points to the real
- variable. This is the better way, but requires a heavier modificaton to
- your program.
-
- By the way, if you want to avoid nasty responses, declare:
- int main()
- ...
- return (0);
-
- rather than
- void main(void)
-
- the latter is not standard despite what certain books may say. I used to
- do it but have seen the error of my ways thanks to this newsgroup.
-
- Hope this helps.
-
- Cheers
- Mike Greenwood
-